
------------------------------------------------------------------------------------
-- GM 命令文件， 方便调试，测试服生效
------------------------------------------------------------------------------------
local ngx          = ngx
local require      = require
local tonumber     = tonumber
local string       = string
local string_lower = string.lower
local math_abs     = math.abs

local log          = require("modules.commhm.log")
local ns_config    = require("modules.business_welfare.welfare_config")
local code_conf    = require("modules.business_welfare.welfare_code")
local uu           = require("modules.commhm.utils")
local ns_account   = require("modules.business_welfare.welfare_bridge")
local gg           = require("modules.business_welfare.welfare_gg")
local welfare_lucky_block   = require('modules.business_welfare.welfare_lucky_block')
local welfare_gift          = require('modules.business_welfare.welfare_gift')
local config     = require("modules.business_welfare.welfare_lua_config")
local utils      = require("modules.business_common.business_utils")
local yxcard     = require("modules.business_welfare.yxcard.welfare_yxcard")


local pairs      = pairs
local math       = math

local ngx = ngx

local error_code = code_conf.error_code

local GM = {
    test = function(a, b)
        log.loggerdebug("====1: a=%% b = %%", a, b)
    end,

    -- gm 添加物品
    add_item = function(id, num)
        local items = {{id=id,num=num}}
        local ret = ns_account.private_account.items_add(items, 'welfare_gm')
        if not ret then
            log.loggerdebug("welfare gm add item error, ret = %%", ret)
            return error_code.ERROR_ADD_ITEM
        end
        return error_code.COMMON_SUCCESS
    end,

    -- 清理礼包数据
    clear_gift_data = function()
        -- 2 获取玩家数据
        local userdata = gg.get_ssdb_cache_info(gg.KEYS.WELFARE_GIFT)
        if not userdata then
            return error_code.ERROR_NO_DATA
        end
        userdata = {}
        gg.set_ssdb_cache_info(gg.KEYS.WELFARE_GIFT, userdata)
        return error_code.COMMON_SUCCESS
    end,

    --礼包测试
    gift_test = function(gift_id, times)
        local userdata = gg.get_ssdb_cache_info(gg.KEYS.WELFARE_GIFT)
        if not userdata then
            return error_code.ERROR_NO_DATA
        end

        local ret, items = welfare_gift.get_gift(userdata, gift_id, times)

        gg.set_ssdb_cache_info(gg.KEYS.WELFARE_GIFT, userdata)

        return ret, {items=items}
    end,

    lucky_block_test = function(layer, times)
        local cfg = config.get_cfg("lucky_block")
        times = times or 0

        local v2 = utils.check_min_version(cfg.v2_limit)
        local activity = v2 and welfare_lucky_block.curr_activity(cfg) or nil

        local type_list = {}
        local dorp_items = {}
        local buff_list = {}
        local userdata = {
            layer = layer,
            items = {},
            activity = activity and activity.id or nil,
        }
        for i=1, times do
            userdata.layer = layer
            local ret, info = welfare_lucky_block.do_start(cfg, userdata) -- items: { pick_type = 2, item = { rate = 15, num = 1, id = 10051, } , }
            if ret == error_code.COMMON_SUCCESS then
                type_list[info.pick_type] = (type_list[info.pick_type] or 0) + 1
                if info.item and info.item.id then
                    dorp_items[info.item.id] = (dorp_items[info.item.id] or 0) + info.item.num
                end
                for _, buff in pairs(info.buff or {}) do
                    buff_list[buff.id] = (buff_list[buff.id] or 0) + 1
                end
            else
                log.loggerdebug("error items: %%", ret)
            end
        end
        local monkey = type_list[1] or 0
        local zhufu = type_list[3] or 0
        local item = type_list[2] or 0
        log.loggerdebug("抽中猴子次数:%%  抽中祝福次数: %%  抽中物品次数:%%   抽中物品列表:%%   抽中buff列表:%%", monkey, zhufu, item, dorp_items, buff_list)
    end,

    -- 银享卡测试--修改激活时间
    yxcard_alter_activetime = yxcard.yxcard_alter_activetime,

    -- 银享卡测试--修改上次邮件提醒时间
    yxcard_alter_lastmailttime = yxcard.yxcard_alter_lastmailttime,
}


local FOO = {}


-- 解参数
local str2params = function(str)
    local params = {}
    for d in string.gmatch(str, '(-?%d+)') do
        table.insert(params, tonumber(d))
    end
    return params
end


FOO.gm_exec = function()
    if ns_config.debug ~= 1 then
        return error_code.COMMON_SUCCESS
    end
    local cmd = ngx.ctx.m_params.gm_cmd
    local str = ngx.ctx.m_params.gm_str
    local params = str2params(str)

    cmd = string.gsub(cmd, ' ', '') --过滤字符串中得空格

    if GM[cmd] then
        local ret = GM[cmd](unpack(params))
        return ret or error_code.COMMON_SUCCESS
    else
        return error_code.COMMON_ERROR
    end
end


return FOO